package com.lucenex.bean;



import java.io.Closeable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import org.apache.log4j.Logger;
import org.apache.lucene.search.highlight.Highlighter;
/**
 * bean 控制类
 * @author zxw
 *
 */
public class BeanConfig implements Closeable{
	
	private Logger log = Logger.getLogger(BeanConfig.class);
	
	static BeanConfig config = new BeanConfig();
	
	//词库
	private Map<String, Dic> dicConfig;
	
	//高亮 value 格式 {method:method,object:object}
	private Map<String, Map<String, Object>> highlightConfig;
	
	
	/**
	 * 初始化bean容器
	 * @return
	 */
	public static synchronized BeanConfig build() {
		//初始化容器
		if(config.dicConfig == null || config.highlightConfig == null) {
			config.dicConfig = new HashMap<>();
			config.highlightConfig = new HashMap<>();
		}
		return config;
	}
	
	/**
	 * 添加
	 * @param plugin
	 */
	public void add(Plugin plugin) {
		Class<? extends Plugin> clas = plugin.getClass();
		Method[] methods = clas.getMethods();
		for (int i = 0; i < methods.length; i++) {
			Method method = methods[i];
			BeanKey beanKey = method.getAnnotation(BeanKey.class);
			if(!method.isAnnotationPresent(BeanKey.class) || beanKey == null) {//该方法不是 bean 或 没有 值
				continue;
			}
			String dataSourceKey = beanKey.value();
			String typeName = method.getAnnotatedReturnType().getType().getTypeName();
			if(typeName.equals(Dic.class.getName())) {//该方法是 词库
				try {
					Dic dic = (Dic) method.invoke(plugin);
					dic.init();
					dicConfig.put(dataSourceKey, dic);
				} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
					log.error("实例化词库", e);
				}
			}else if(typeName.equals(Highlighter.class.getName())) {//高亮
				Map<String, Object> map = new HashMap<>();
				map.put("method", method);
				map.put("object", plugin);
				highlightConfig.put(dataSourceKey, map);
			}
			
		}
	}
	
	/**
	 * 获取一个词库
	 * @param dataSourceKey
	 * @return
	 */
	public Dic getDic(String dataSourceKey) {
		if(dicConfig != null) {
			return dicConfig.get(dataSourceKey);
		}
		return null;
	}
	
	/**
	 * 获取一个高亮
	 * @param dataSourceKey
	 * @return
	 */
	public Map<String, Object> getHigh(String dataSourceKey){
		if(highlightConfig != null) {
			return highlightConfig.get(dataSourceKey);
		}
		return null;
	}

	@Override
	public void close(){
		if(dicConfig != null && highlightConfig != null) {
			Set<Entry<String,Dic>> set = dicConfig.entrySet();
			for (Entry<String, Dic> entry : set) {
				entry.getValue().getThesaurus().clear();
			}
			dicConfig.clear();
			Set<Entry<String,Map<String,Object>>> entrySet = highlightConfig.entrySet();
			for (Entry<String, Map<String, Object>> entry : entrySet) {
				entry.getValue().clear();
			}
			highlightConfig.clear();
		}
	}

}
